package com.zjc.yohoshop.mvp.view.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.AddAddressEntity;
import com.zjc.yohoshop.bean.AddressListEntity;
import com.zjc.yohoshop.bean.DelAddressEntity;
import com.zjc.yohoshop.dao.USerService;
import com.zjc.yohoshop.di.compont.DaggerAddressListFragmentCompont;
import com.zjc.yohoshop.di.modules.AddressListFragmentModules;
import com.zjc.yohoshop.mvc.control.SPHelperSave;
import com.zjc.yohoshop.mvc.view.FragmentActivity;
import com.zjc.yohoshop.mvp.contract.AddressListFragmentContract;
import com.zjc.yohoshop.mvp.persenter.AddressListFragmentPersenter;
import com.zjc.yohoshop.mvp.view.adapter.AddressListAdapter;

import org.w3c.dom.Text;

public class AddressListFragment extends BaseFragment<AddressListFragmentPersenter> implements AddressListFragmentContract.AddressListFragmentIView, View.OnClickListener {

    private String user_id;
    private View view;
    //TODO 地址管理
    private RecyclerView rv_address_list;
    private AddressListAdapter adapter;
    //TODO 添加收过地址/编辑收货地址


    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_address_list, null);
        return view;
    }

    @Override
    public void init(View view) {

        rv_address_list = view.findViewById(R.id.rv_address_list);
        adapter = new AddressListAdapter(R.layout.item_address_list);
        rv_address_list.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_address_list.setAdapter(adapter);

    }

    @Override
    public void initData() {
        p.addressListRequest(user_id);
    }

    @Override
    public void setupDagger() {
        DaggerAddressListFragmentCompont.builder()
                .addressListFragmentModules(new AddressListFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {

        view.findViewById(R.id.iv_address_list_back).setOnClickListener(this);
        view.findViewById(R.id.btn_address_list_add).setOnClickListener(this);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View adapterView, int position) {
                switch (adapterView.getId()){
                    case R.id.cb_item_address_list_address_default://设为默认
                        for (int i = 0; i < adapter.getData().size(); i++){
                            adapter.getData().get(i).address_default = "0";
                        }
                        adapter.getData().get(position).address_default = "1";
                        SPHelperSave.getInstance().setAddress_default(adapter.getData().get(position).address_id);
                        adapter.notifyDataSetChanged();
                        break;
                    case R.id.tv_item_address_address_edit://编辑
                        Intent edit = new Intent(getActivity(), FragmentActivity.class);
                        edit.putExtra("type", "addaddress");
                        edit.putExtra("address_id", adapter.getData().get(position).address_id);
                        getActivity().startActivity(edit);
                        break;
                    case R.id.tv_item_address_address_del://删除
                        p.delAddressRequest(adapter.getData().get(position).address_id);
                        break;
                }
            }
        });

    }

    @Override
    public void addAddressSuccess(AddAddressEntity entity) {

    }

    @Override
    public void addressListSuccess(AddressListEntity entity) {
        if (adapter != null){
            String address_id = SPHelperSave.getInstance().getAddress_default();
            if (!TextUtils.isEmpty(address_id)){
                for (AddressListEntity.ValuesBean valuesBean : entity.values){
                    if (address_id.equals(valuesBean.address_id)){
                        valuesBean.address_default = "1";
                    }else{
                        valuesBean.address_default = "0";
                    }
                }
            }
            adapter.setNewData(entity.values);
        }
    }

    @Override
    public void delAddressSuccess(DelAddressEntity entity) {
        p.addressListRequest(user_id);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Cursor cursor_user = USerService.getInstance().query("user", null, null);
        if (cursor_user.moveToNext()) {
            user_id = cursor_user.getString(cursor_user.getColumnIndex("user_id"));
        } else {
            Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        p.addressListRequest(user_id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_address_list_back://返回上一页
                getActivity().finish();
                break;
            case R.id.btn_address_list_add://添加新地址
                if (adapter.getData().size() >= 5 ){
                    Toast.makeText(getContext(), "您最多添加5个收货地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent edit = new Intent(getActivity(), FragmentActivity.class);
                edit.putExtra("type", "addaddress");
                getActivity().startActivity(edit);
                break;
        }
    }
}
